package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.AuditOpinionService;
import com.paic.mhis.hcpms.audit.biz.service.AuditWorkFlowService;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCommonService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dao.AuditDispatchDao;
import com.paic.mhis.hcpms.audit.dao.AuditOpinionDao;
import com.paic.mhis.hcpms.audit.dao.AuditSurveyReviewDao;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.audit.dto.AuditCommDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.audit.dto.AuditProblemsDTO;
import com.paic.mhis.hcpms.audit.dto.AuditiCaseDTO;
import com.paic.mhis.hcpms.audit.dto.HcpmsWFHistoryDTO;
import com.paic.mhis.hcpms.audit.util.GetFormDateUtil;
import com.paic.mhis.hcpms.common.document.biz.service.IDocumentService;
import com.paic.mhis.hcpms.common.document.dto.DocSaveParamterDTO;
import com.paic.mhis.hcpms.common.document.util.Constant;
import com.paic.mhis.hcpms.common.storage.biz.service.impl.HcpmsStorageServiceImpl;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;

/**
 * 
 * @author EX-LUORONG001
 * 
 */
@Component("auditOpinionServiceImpl")
public class AuditOpinionServiceImpl implements AuditOpinionService {
	@Autowired
	private AuditOpinionDao auditOpinionDao;

	@Autowired
	private AuditDispatchDao auditDispatchDao;

	@Autowired
	private AuditSurveyReviewDao auditSurveyReviewDao;

	@Autowired
	private CommonDAO commonDAO;

	@Autowired
	private IntegrityMedicalService integrityMedicalService;

	@Autowired
	private AuditWorkFlowService auditWorkFlowService; // 流程服务

	@Autowired
	private HcpmsStorageServiceImpl hcpmsStorageService;// 暂存临时表

	@Autowired
	private ICaseCommonService commonService;

	@Autowired
	private ICaseCommonService caseCommonServiceImpl;

	@Autowired
	private IDocumentService documentService;

	@Override
	public Map<String, Object> getOpinionList(AuditOpinionDTO opinionDTO,
			String type1, String type2) {
		String affirmDeadLine = "";
		String caseNo = opinionDTO.getProblemNo();
		AuditiCaseDTO parameter = new AuditiCaseDTO();
		parameter.setCaseCode(caseNo);
		AuditiCaseDTO caseBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(parameter);

		if (caseBean != null) {
			Date deadLine = caseBean.getAffirmDeadLine();
			affirmDeadLine = GetFormDateUtil.getStrFromDate(deadLine);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		List<AuditOpinionDTO> opinion = new ArrayList<AuditOpinionDTO>();
		if (type1.equals(AuditConstants.opinionType_1)) {
			opinion = auditOpinionDao
					.queryAuditOpinionDTOByProblemNo(opinionDTO);
		} else if (type1.equals(AuditConstants.opinionType_3)) {
			opinion = auditOpinionDao.queryOpinionDTOByProblemNo(opinionDTO);
		} else if (type1.equals(AuditConstants.opinionType_12)) {
			opinion = auditOpinionDao.queryOpinions(opinionDTO);
		}

		if (null != opinion && opinion.size() > 0) {
			for (int i = 0; i < opinion.size(); i++) {
				if (null != opinion.get(i).getOpinionType()
						&& !"".equals(opinion.get(i).getOpinionType())
						&& opinion.get(i).getOpinionType().equals(type1)) {

					AuditOpinionDTO currentOpionion = opinion.get(i);
					currentOpionion.setAffirmDeadline(affirmDeadLine);
					map.put("treatmentOpinion", currentOpionion);

				} else if (null != opinion.get(i).getOpinionType()
						&& !"".equals(opinion.get(i).getOpinionType())
						&& opinion.get(i).getOpinionType().equals(type2)) {
					AuditOpinionDTO currentOpionion = opinion.get(i);
					currentOpionion.setAffirmDeadline(affirmDeadLine);
					map.put("auditOpinion", currentOpionion);
				}
			}
		} else {
			map.put("treatmentOpinion", null);
			map.put("auditOpinion", null);
		}

		return map;
	}

	/**
	 * 事件调查（回退） type:标识 调查还是复核 0，调查，1，复核 flag: 0，事件，1，案件
	 */
	public Map<String, Object> loadOpinionProjectList(
			AuditOpinionDTO opinionDTO, String type1, String type2,
			String type, String flag, String userName, boolean state) {

		String caseNo = opinionDTO.getProblemNo();
		AuditiCaseDTO parameter = new AuditiCaseDTO();
		parameter.setCaseCode(caseNo);
		AuditiCaseDTO caseBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(parameter);
		String affirmDeadLine = "";
		if (caseBean != null) {
			Date deadLine = caseBean.getAffirmDeadLine();
			affirmDeadLine = GetFormDateUtil.getStrFromDate(deadLine);
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("treatmentOpinion", "");
		map.put("auditOpinion", "");
		map.put("audit", "");
		map.put("tempFlag", "2");

		String tempFlag = "";
		String appointFirst = "";
		// 当前分配B岗
		String appointSecond = "";
		if ("0".equals(flag)) {
			opinionDTO.setTaskType("1");
			opinionDTO.setOpinionType(AuditConstants.opinionType_2);
			// 获取调查审核意见0
			AuditOpinionDTO auditOpinionDTO = this
					.queryAuditOpinionDTO(opinionDTO);
			// 当前分配A岗
			appointFirst = auditOpinionDTO.getAppointFirst();
			// 当前分配B岗
			appointSecond = auditOpinionDTO.getAppointSecond();
			// 获取调查结论意见、调查结论复核意见0
			List<AuditOpinionDTO> opinion = auditOpinionDao
					.queryOpinionDTOByProblemNo(opinionDTO);
			// 获取调查结论审核意见0
			AuditOpinionDTO auditDto = auditOpinionDao.getOpinion(opinionDTO);

			AuditProblemsDTO dto = new AuditProblemsDTO();
			dto.setProblemCode(opinionDTO.getProblemNo());
			AuditProblemsDTO rsl = (AuditProblemsDTO) commonDAO.get(
					"auditProblemsDao.queryAuditProblemsDetail", dto);
			tempFlag = rsl.getTempFlag();

			map = this.processHistoryShow(opinion, type, type1, type2,
					appointFirst, appointSecond, auditDto, map, state,
					tempFlag, affirmDeadLine);
		} else {
			opinionDTO.setTaskType("2");
			opinionDTO.setOpinionType(AuditConstants.opinionType_10);
			// 获取稽核分配意见1
			AuditOpinionDTO auditOpinionDTO = this
					.queryAuditOpinionDTO(opinionDTO);
			// 当前分配A岗
			appointFirst = auditOpinionDTO.getAppointFirst();
			// 当前分配B岗
			appointSecond = auditOpinionDTO.getAppointSecond();
			// 获取稽核结论复核意见、稽核结论审核意见1
			List<AuditOpinionDTO> opinion = auditOpinionDao
					.queryOpinionList(opinionDTO);
			// 获取稽核结论意见1
			AuditOpinionDTO auditDto = auditOpinionDao
					.getOpinionDto(opinionDTO);

			AuditCaseDTO dto = new AuditCaseDTO();
			dto.setCaseCode(opinionDTO.getProblemNo());
			tempFlag = (String) commonDAO.get("audticase.getCaseInfo", dto);

			map = this.processHistoryShow(opinion, type, type1, type2,
					appointFirst, appointSecond, auditDto, map, state,
					tempFlag, affirmDeadLine);
		}
		return map;
	}

	public Map<String, Object> processHistoryShow(
			List<AuditOpinionDTO> opinion, String type, String type1,
			String type2, String appointFirst, String appointSecond,
			AuditOpinionDTO auditDto, Map<String, Object> map, boolean state,
			String tempFlag, String affirmDeadLine) {
		if (null != opinion && opinion.size() > 0) {
			{
				for (int i = 0; i < opinion.size(); i++) {
					AuditOpinionDTO currentOpionion = opinion.get(i);
					currentOpionion.setAffirmDeadline(affirmDeadLine);
					state = this.processOpinion(currentOpionion, type, type1,
							type2, appointFirst, appointSecond, auditDto, map,
							state, tempFlag);
				}
			}
		}
		return map;
	}

	public boolean processOpinion(AuditOpinionDTO opinion, String type,
			String type1, String type2, String appointFirst,
			String appointSecond, AuditOpinionDTO auditDto,
			Map<String, Object> map, boolean state, String tempFlag) {
		if (type1.equals(opinion.getOpinionType()) && type.equals("0")
				&& appointFirst.equals(opinion.getDealUserName())) {
			if ("2".equals(tempFlag)) {
				map.put("treatmentOpinion", opinion);
			} else {
				state = true;
				map.put("treatmentOpinion", opinion);
				map.put("audit", auditDto);
				map.put("tempFlag", "1");
			}
		} else if (type1.equals(opinion.getOpinionType()) && type.equals("1")
				&& appointFirst.equals(opinion.getDealUserName())) {
			state = true;
			map.put("treatmentOpinion", opinion);
		}
		if (state) {
			Map<String, Object> m = this.processOpinionShow(opinion, type,
					appointSecond, auditDto, map, tempFlag);
			map.putAll(m);
		}
		return state;
	}

	public Map<String, Object> processOpinionShow(AuditOpinionDTO opinion,
			String type, String appointSecond, AuditOpinionDTO auditDto,
			Map<String, Object> map, String tempFlag) {
		if (appointSecond.equals(opinion.getDealUserName())) {
			if ("0".equals(type)) {
				map.put("auditOpinion", opinion);
			} else {
				if ("2".equals(tempFlag)) {
					map.put("auditOpinion", opinion);
				} else {
					map.put("auditOpinion", opinion);
					map.put("audit", auditDto);
					map.put("tempFlag", "1");
				}
			}
		}
		return map;
	}

	@Transactional
	public Map<String, Object> addOpinion(AuditOpinionDTO opinionDTO) {
		Map<String, Object> map = new HashMap<String, Object>();

		Date date = new Date();
		SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
		String dates = sft.format(date);

		opinionDTO.setDealDate(dates);
		auditOpinionDao.addOpinion(opinionDTO);
		map.put("state", AuditConstants.FLAG_TURE);
		map.put("msg", AuditConstants.INSERT_SUCCESS);
		return map;
	}

	@Transactional
	public Map<String, Object> updateBatchCase(AuditOpinionDTO opinionDTO,
			UserDetails user, Map<String, Object> map) throws Exception {
		int count = 0;
		AuditCaseDTO dto = new AuditCaseDTO();

		String caseCodes[] = opinionDTO.getCaseCodes();
		opinionDTO.setDealUserName(user.getUsername());
		opinionDTO.setOpinionType(AuditConstants.opinionType_12);
		opinionDTO.setTaskType(AuditConstants.taskType_02);
		for (String caseCode : caseCodes) {
			dto.setCaseCode(caseCode);
			// 获取当前案件状态：==‘3’才可以复核
			AuditCaseDTO rsl = (AuditCaseDTO) commonDAO.get(
					"audticase.getCaseState", dto);
			if (rsl.getCaseState().equals(AuditConstants.auditCaseStatus_3)) {
				JSONObject transferJson = commonService
						.queryBatchOpertionAuditCasePoint(caseCode,
								user.getUsername());// 获取临时扣分
				opinionDTO.setOnlySave("0");
				opinionDTO.setProblemNo(caseCode);
				this.processCaseReview(opinionDTO, transferJson, user, map);
				// 医务人员、参保人进行数据添加
				commonService.batchOpertionMedicalCommon(caseCode,
						AuditConstants.taskType_02, user.getUsername());
				count++;
			}
		}
		map.put("state", AuditConstants.FLAG_TURE);
		map.put("msg", "操作成功" + count + "条，失败" + (caseCodes.length - count)
				+ "条!");
		return map;
	}

	/**
	 * 处理案件复核的逻辑(回退)
	 */
	@Transactional
	public Map<String, Object> processCaseReview(AuditOpinionDTO opinionDTO,
			JSONObject transferJson, UserDetails user, Map<String, Object> map)
			throws Exception {
		String currentOpinon = "";
		// 更新案件的状态
		AuditCaseDTO caseDTO = new AuditCaseDTO();
		caseDTO.setCaseCode(opinionDTO.getProblemNo());

		// onlySave：1，暂存；0，保存
		if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "0".equals(opinionDTO.getOnlySave())) {
			// 表示 回退的稽核复核
			if (StringUtils.hasText(opinionDTO.getFlag())
					&& AuditConstants.TEMP_FLAG_1.equals(opinionDTO.getFlag())) {
				currentOpinon = opinionDTO.getOpinionState(); // 回退的复核意见
			} else {
				currentOpinon = opinionDTO.getOpinion();
			}

			if (StringUtils.hasText(currentOpinon)
					&& AuditConstants.opinionResult_1.equals(currentOpinon)) { // AB岗位意见一致
				caseDTO.setCaseState(AuditConstants.auditCaseStatus_4);
				Date deadLineDate = new Date();
				String noticDateStr = GetFormDateUtil
						.getStrFromDate(deadLineDate);
				caseDTO.setNotifyDate(noticDateStr);
				caseDTO.setNotifyDoc(noticDateStr);
				caseDTO.setAffirmDeadline(opinionDTO.getAffirmDeadline());

			} else if (StringUtils.hasText(currentOpinon)
					&& AuditConstants.opinionResult_0.equals(currentOpinon)) { // AB岗不一致
				caseDTO.setCaseState(AuditConstants.auditCaseStatus_5);
			}
			caseDTO.setTempFlag(AuditConstants.TEMP_FLAG_0);
			this.auditDispatchDao.updateAuditCaseDTO(caseDTO);

			if ("1".equals(opinionDTO.getFlag()))// "1"稽核
			{
				opinionDTO.setOpinion(opinionDTO.getOpinionState());
			}
			int count = auditOpinionDao.getOpinionCount(opinionDTO);
			if (count > 0) {
				auditOpinionDao.updateProblemOpinion(opinionDTO);
				map.put("state", AuditConstants.FLAG_TURE);
				map.put("msg", AuditConstants.UPDATE_SUCCESS);
			} else {
				map = this.addOpinion(opinionDTO);
			}

			if (null != opinionDTO.getOldOpinionDesc()
					&& !"".equals(opinionDTO.getOldOpinionDesc())
					&& null != opinionDTO.getNewOpinionDesc()
					&& !"".equals(opinionDTO.getNewOpinionDesc())) {
				opinionDTO.setOpinionType(AuditConstants.opinionType_11);
				opinionDTO.setOpinionDesc(opinionDTO.getNewOpinionDesc());
				opinionDTO.setOriginalOpinionDesc(opinionDTO
						.getOldOpinionDesc());
				opinionDTO.setOpinion(null);

				Map<String, Object> maps = new HashMap<String, Object>();
				DocSaveParamterDTO bean = new DocSaveParamterDTO();
				maps.put("opinionDesc", opinionDTO.getOpinionDesc());
				bean.setMap(maps);
				bean.setCreateUser(user.getUsername());
				bean.setDocType(Constant.docType_05);
				bean.setTaskNo(opinionDTO.getProblemNo());
				bean.setTaskType(Constant.taskType_05);

				if (!opinionDTO.getOpinionDesc().equals(
						opinionDTO.getOriginalOpinionDesc())) {
					auditOpinionDao.updateProblem(opinionDTO);// 修改稽核意见说明
					documentService.updateDocContentDTO(bean);// 同步修改稽核告知函意见说明
				}
			}
			// 删除暂存信息：
			this.deleteStorageData(opinionDTO.getProblemNo(),
					AuditConstants.jhModule, AuditConstants.review,
					AuditConstants.opinionType_12, user.getUsername());
			// 扭转流程
			opinionDTO.setOpinion(currentOpinon);
			this.processWorkFlow(opinionDTO, user);
		} else if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "1".equals(opinionDTO.getOnlySave())) {
			// 暂存（添加）
			this.saveStorageData(opinionDTO.getProblemNo(),
					AuditConstants.jhModule, AuditConstants.review,
					AuditConstants.opinionType_12, user.getUsername(),
					opinionDTO, map);
		}
		// 扣分
		this.getAuditCasePoint(opinionDTO, transferJson, user.getUsername());
		return map;
	}

	/**
	 * 处理流程,目前1306需求中A的意见都是异常，则只需考虑B岗复核意见，
	 * 如果复核意见同意则代表AB岗意见相同，流程直接走到医院确认，如果B岗意见是保留意见，直接到审核岗位去了
	 * 
	 * @throws Exception
	 */
	@Transactional
	private void processWorkFlow(AuditOpinionDTO opinionDTO, UserDetails user)
			throws Exception {
		String onlySave = opinionDTO.getOnlySave();
		String flag = opinionDTO.getFlag();
		if (StringUtils.hasText(flag)
				&& AuditConstants.TEMP_FLAG_1.equals(flag)) { // 表示是退回的复核意见
			opinionDTO.setOpinion(opinionDTO.getOpinionState());
		}

		if (StringUtils.hasText(onlySave)
				&& AuditConstants.ONLY_SAVE_0.equals(onlySave)) {// 表示保存
			String opinion = opinionDTO.getOpinion();
			if (StringUtils.hasText(opinion)
					&& AuditConstants.opinionResult_1.equals(opinion)) { // AB岗位意见一致

				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_REVIEW_TYPE,
						AuditConstants.AUDIT_CASE_REVIEW_TYPE_01);
				this.auditWorkFlowService.processWorkFlow(
						opinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);

				// 流程扭转到医院确认
			} else if (StringUtils.hasText(opinion)
					&& AuditConstants.opinionResult_0.equals(opinion)) { // AB岗不一致

				// 流程扭转到审核岗位进行审核
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_REVIEW_TYPE,
						AuditConstants.AUDIT_CASE_REVIEW_TYPE_02);
				this.auditWorkFlowService.processWorkFlow(
						opinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);
			}

		}

	}

	public Map<String, Object> getOpinion(AuditOpinionDTO opinionDTO) {
		Map<String, Object> map = new HashMap<String, Object>();
		AuditOpinionDTO opinion = auditOpinionDao
				.getAuditOpinionDTOByProblemNo(opinionDTO);

		map.put("problem", opinion);
		return map;
	}

	public AuditOpinionDTO getAuditOpinion(AuditOpinionDTO opinionDTO) {
		AuditOpinionDTO opinion = auditOpinionDao.getOpinion(opinionDTO);
		return opinion;
	}

	public void updateOpinion(AuditOpinionDTO opinionDTO) {
		opinionDTO.setOpinionDesc(opinionDTO.getNewOpinionDesc());
		opinionDTO.setOriginalOpinionDesc(opinionDTO.getOldOpinionDesc());
		if (!opinionDTO.getOpinionDesc().equals(
				opinionDTO.getOriginalOpinionDesc())) {
			auditOpinionDao.updateOpinion(opinionDTO);
		}
	}

	/**
	 * 根据意见物理id 或者任务编码或者任务类型或者意见类型或者删除标准加载意见对象
	 */
	public AuditOpinionDTO queryAuditOpinionDTO(AuditOpinionDTO opinionDTO) {

		String caseNo = opinionDTO.getProblemNo();

		AuditiCaseDTO queryCaseBean = new AuditiCaseDTO();
		queryCaseBean.setCaseCode(caseNo);
		AuditiCaseDTO returnBean = this.auditSurveyReviewDao
				.loadAuditiCaseDTOByCode(queryCaseBean);

		AuditOpinionDTO opinionBean = this.auditOpinionDao
				.queryOpionionDTO(opinionDTO);
		if (returnBean != null) {
			String affirmDeadLine = GetFormDateUtil.getStrFromDate(returnBean
					.getAffirmDeadLine());
			if (opinionBean != null) {
				opinionBean.setAffirmDeadline(affirmDeadLine);
			}

		}

		return opinionBean;
	}

	/**
	 * 处理事件调查审核:新增一条意见类型意见类型(调查结论{ 是否稽核}) 且事件状态更新为调查待复核
	 */
	@Transactional
	public Map<String, Object> processProblemAudit(AuditOpinionDTO opinionDTO,
			JSONObject transferJson, String userName, UserDetails user,
			Map<String, Object> map) throws Exception {
		// 更新事件的状态
		AuditProblemsDTO problemBean = new AuditProblemsDTO();
		problemBean.setProblemCode(opinionDTO.getProblemNo());
		// onlySave：1，暂存；0，保存
		if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "0".equals(opinionDTO.getOnlySave())) {
			problemBean.setProblemStatus(AuditConstants.problemStatus_5);
			problemBean.setTempFlag(AuditConstants.TEMP_FLAG_0);

			this.auditDispatchDao.updateAuditProblemBean(problemBean);

			int count = auditOpinionDao.getOpinionCount(opinionDTO);
			if (count <= 0) {
				map = this.addOpinion(opinionDTO);
			} else {
				auditOpinionDao.updateProblemOpinion(opinionDTO);
				map.put("state", AuditConstants.FLAG_TURE);
				map.put("msg", AuditConstants.UPDATE_SUCCESS);
			}

			// onlySave：1，暂存；0，保存
			// 流程扭转{当为“保存操作”时，才会扭转流程，暂存不会扭转流程}
			AuditOpinionDTO op = new AuditOpinionDTO();
			op.setOpinionType(AuditConstants.opinionType_2);
			op.setTaskType("1");
			op.setProblemNo(opinionDTO.getProblemNo());
			AuditOpinionDTO resultOpinion = this.getOpinionByType(op);

			// 删除暂存信息：
			this.deleteStorageData(opinionDTO.getProblemNo(),
					AuditConstants.sjModule, AuditConstants.audit,
					AuditConstants.opinionType_3, userName);
			// 注：如果无法获取经办岗2，则流程无法下发
			auditWorkFlowService.processWorkFlow(opinionDTO.getProblemNo(),
					user, resultOpinion.getAppointSecond().toUpperCase(), null,
					AuditConstants.TASK_TYPE_PROBLEM);
		} else if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "1".equals(opinionDTO.getOnlySave())) {
			// 暂存（添加）
			this.saveStorageData(opinionDTO.getProblemNo(),
					AuditConstants.sjModule, AuditConstants.audit,
					AuditConstants.opinionType_3, userName, opinionDTO, map);
		}
		// 扣分
		this.getAuditCasePoint(opinionDTO, transferJson, userName);

		return map;
	}

	@Transactional
	public Map<String, Object> processBackProblemReview(
			AuditOpinionDTO opinionDTO, JSONObject transferJson,
			String userName, UserDetails user, Map<String, Object> map)
			throws Exception {
		// 更新事件的状态
		AuditProblemsDTO problemBean = new AuditProblemsDTO();
		problemBean.setProblemCode(opinionDTO.getProblemNo());
		// onlySave：1，暂存；0，保存
		if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "0".equals(opinionDTO.getOnlySave())) {
			problemBean.setProblemStatus(AuditConstants.problemStatus_6);
			problemBean.setTempFlag(AuditConstants.TEMP_FLAG_0);
			this.auditDispatchDao.updateAuditProblemBean(problemBean);

			int count = auditOpinionDao.getOpinionCount(opinionDTO);
			if (count > 0) {
				opinionDTO.setOpinion(opinionDTO.getOpinionState());
				auditOpinionDao.updateProblemOpinion(opinionDTO);
				map.put("state", AuditConstants.FLAG_TURE);
				map.put("msg", AuditConstants.UPDATE_SUCCESS);
			} else {
				opinionDTO.setOpinion(opinionDTO.getOpinionState());
				map = this.addOpinion(opinionDTO);
			}

			if (null != opinionDTO.getOldOpinionDesc()
					&& !"".equals(opinionDTO.getOldOpinionDesc())
					&& null != opinionDTO.getNewOpinionDesc()
					&& !"".equals(opinionDTO.getNewOpinionDesc())) {
				opinionDTO.setOpinionType(AuditConstants.opinionType_3);
				opinionDTO.setOpinionDesc(opinionDTO.getNewOpinionDesc());
				opinionDTO.setOriginalOpinionDesc(opinionDTO
						.getOldOpinionDesc());
				if (!opinionDTO.getOpinionDesc().equals(
						opinionDTO.getOriginalOpinionDesc())) {
					auditOpinionDao.updateProblem(opinionDTO);
				}
			}
			// 删除暂存信息：
			this.deleteStorageData(opinionDTO.getProblemNo(),
					AuditConstants.sjModule, AuditConstants.review,
					AuditConstants.opinionType_4, userName);
			// 流程扭转{当为“保存操作”时，才会扭转流程，暂存不会扭转流程}
			auditWorkFlowService.processWorkFlow(opinionDTO.getProblemNo(),
					user, null, AuditConstants.TASK_TYPE_PROBLEM);
		} else if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "1".equals(opinionDTO.getOnlySave())) {
			// 暂存（添加）
			this.saveStorageData(opinionDTO.getProblemNo(),
					AuditConstants.sjModule, AuditConstants.review,
					AuditConstants.opinionType_4, userName, opinionDTO, map);
		}
		this.getAuditCasePoint(opinionDTO, transferJson, userName);
		return map;
	}

	@Override
	@Transactional
	public Map<String, Object> approvementAuditOpinion(
			AuditOpinionDTO opinionDTO, UserDetails user,
			Map<String, Object> map) throws Exception {

		if ("1".equals(opinionDTO.getOnlySave())) {

			HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
			tempObject.setBusinessId(opinionDTO.getProblemNo());
			tempObject.setModuleType(AuditConstants.jhModule);
			tempObject.setChildModuleType(AuditConstants.approvement);
			tempObject.setBusinessType(AuditConstants.opinionType_15);
			tempObject.setUserId(user.getUsername());
			tempObject.setDataMap(opinionDTO);
			this.hcpmsStorageService.saveStorageData(tempObject, false);
			map.put("msg", "操作成功了!");
			return map;

		}

		opinionDTO.setOpinionType(opinionDTO.getOpinionTypeCurrent());
		opinionDTO.setTaskType(AuditConstants.taskType_02);
		List<AuditOpinionDTO> al = commonDAO.query(
				"opinoin.queryAuditOpinoinByParameter", opinionDTO);
		if (al != null && al.size() >= 1) {
			opinionDTO.setId(al.get(0).getId());
			auditOpinionDao.updateOpinion(opinionDTO);
			map.put("msg", AuditConstants.UPDATE_SUCCESS);
		} else {
			Date date = new Date();
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			String dates = sft.format(date);
			opinionDTO.setDealDate(dates);
			opinionDTO.setIsDeleted(AuditConstants.isDeleted);
			auditOpinionDao.addOpinion(opinionDTO);
			map.put("msg", AuditConstants.INSERT_SUCCESS);
		}

		if ("0".equals(opinionDTO.getOnlySave())) {
			AuditCaseDTO dto = new AuditCaseDTO();
			// 同意 :1 ,不同意 :0
			if ("1".equals(opinionDTO.getOpinion())) {
				// /审批保存同意流程
				dto.setCaseState(opinionDTO.getStatusAfter());
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_APPROVE_TYPE,
						AuditConstants.AUDIT_CASE_APPROVE_TYPE_01);
				this.auditWorkFlowService.processWorkFlow(
						opinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);
				// 审批同意后 流程结束 扣分信息转移
				Map<String, Object> paramMap = new HashMap<String, Object>();
				AuditCaseDTO adto = new AuditCaseDTO();
				adto.setCaseCode(opinionDTO.getProblemNo());
				adto = (AuditCaseDTO) commonDAO.get("audticase.getCase", adto);
				paramMap.put("warnNo", opinionDTO.getProblemNo());
				paramMap.put("userName", user.getUsername());
				paramMap.put("happenDate", adto.getPeriodEndDates());
				paramMap.put("caseType", "02");
				integrityMedicalService.addIntegrityPointTemps(paramMap);
			} else if ("0".equals(opinionDTO.getOpinion())) {
				dto.setCaseState(AuditConstants.auditCaseStatus_5);

				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_APPROVE_TYPE,
						AuditConstants.AUDIT_CASE_APPROVE_TYPE_02);
				this.auditWorkFlowService.processWorkFlow(
						opinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);
			}
			// /////////这里还没确认不同意的情况下 案件状态是什么 ........
			dto.setCaseCode(opinionDTO.getProblemNo());
			commonDAO.update("audticase.updateCaseStatus", dto);

			// 删除临时表
			HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
			tempObject.setBusinessId(opinionDTO.getProblemNo());
			tempObject.setModuleType(AuditConstants.jhModule);
			tempObject.setChildModuleType(AuditConstants.approvement);
			tempObject.setBusinessType(AuditConstants.opinionType_15);
			tempObject.setUserId(user.getUsername());
			this.hcpmsStorageService.deleteStorageData(tempObject);

		}
		return map;
	}

	/**
	 * coder shi
	 */
	@Transactional
	public Map<String, Object> approvementAudits(AuditOpinionDTO opinionDTO,
			UserDetails user, Map<String, Object> map, String[] caseCodes)
			throws Exception {
		// 和AuditManageRollBackDTO对象没有关系，只是用于记录返回结果
		StringBuffer sbf = new StringBuffer();
		for (String caseCode : caseCodes) {
			// 记录返回信息
			AuditCaseDTO caseInfo = new AuditCaseDTO();
			caseInfo.setCaseCode(caseCode);
			caseInfo = (AuditCaseDTO) commonDAO.get("audticase.getCase",
					caseInfo);
			if (!AuditConstants.auditCaseStatus_6.equals(caseInfo
					.getCaseState())) {
				// 此对象用于记录返回信息
				sbf.append("案件编号：").append(caseCode);
				sbf.append(
						caseInfo.getCaseState().equals(
								AuditConstants.auditCaseStatus_7) ? "该案件已结束！"
								: "该案件已被撤回！").append("\n");

				continue;
			}
			AuditOpinionDTO tempOpinionDTO = new AuditOpinionDTO();
			BeanUtils.copyProperties(opinionDTO, tempOpinionDTO);
			tempOpinionDTO.setProblemNo(caseCode);
			tempOpinionDTO.setOpinionType(opinionDTO.getOpinionTypeCurrent());
			tempOpinionDTO.setTaskType(AuditConstants.taskType_02);
			Date date = new Date();
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			String dates = sft.format(date);
			tempOpinionDTO.setDealDate(dates);
			tempOpinionDTO.setIsDeleted(AuditConstants.isDeleted);
			auditOpinionDao.addOpinion(tempOpinionDTO);

			AuditCaseDTO dto = new AuditCaseDTO();
			// 0：不同意，1：同意
			if ("1".equals(opinionDTO.getOpinion())) {
				dto.setCaseState(AuditConstants.auditCaseStatus_7);
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_APPROVE_TYPE,
						AuditConstants.AUDIT_CASE_APPROVE_TYPE_01);
				this.auditWorkFlowService.processWorkFlow(
						tempOpinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);
				// 审批同意后 流程结束 扣分信息转移
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("warnNo", caseCode);
				paramMap.put("userName", user.getUsername());
				paramMap.put("happenDate", caseInfo.getPeriodEndDates());
				paramMap.put("caseType", "02");
				integrityMedicalService.addIntegrityPointTemps(paramMap);
			} else if ("0".equals(opinionDTO.getOpinion())) {
				dto.setCaseState(AuditConstants.auditCaseStatus_4);
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_APPROVE_TYPE,
						AuditConstants.AUDIT_CASE_APPROVE_TYPE_02);
				this.auditWorkFlowService.processWorkFlow(
						opinionDTO.getProblemNo(), user, condition,
						AuditConstants.TASK_TYPE_CASE);
			}

			dto.setCaseCode(caseCode);
			commonDAO.update("audticase.updateCaseStatus", dto);
			// 删除临时表
			HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
			tempObject.setBusinessId(caseCode);
			tempObject.setModuleType(AuditConstants.jhModule);
			tempObject.setChildModuleType(AuditConstants.approvement);
			tempObject.setBusinessType(AuditConstants.opinionType_15);
			tempObject.setUserId(user.getUsername());
			this.hcpmsStorageService.deleteStorageData(tempObject);

		}// 循环结束
		Map<String, Object> returnMap = new HashMap<String, Object>();
		if (sbf.toString().length() == 0) {
			returnMap.put("msg", "批量审批完成！");
		} else {
			returnMap.put("msg", sbf.toString());
		}
		return returnMap;
	}

	@Transactional
	public Map<String, Object> approvementProblemOpinion(
			AuditOpinionDTO opinionDTO, JSONObject transferJson,
			UserDetails user, Map<String, Object> map) throws Exception {
		opinionDTO.setOpinionType(opinionDTO.getOpinionTypeCurrent());
		opinionDTO.setTaskType(AuditConstants.taskType_01);
		List<AuditOpinionDTO> al = commonDAO.query(
				"opinoin.queryAuditOpinoinByParameter", opinionDTO);
		if (al != null && al.size() >= 1) {
			opinionDTO.setId(al.get(0).getId());
			auditOpinionDao.updateOpinion(opinionDTO);
			map.put("msg", AuditConstants.UPDATE_SUCCESS);
		} else {
			Date date = new Date();
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			String dates = sft.format(date);
			opinionDTO.setDealDate(dates);
			opinionDTO.setIsDeleted(AuditConstants.isDeleted);
			auditOpinionDao.addOpinion(opinionDTO);
			map.put("msg", AuditConstants.INSERT_SUCCESS);
		}
		// 0表示保存1表示暂存
		if ("0".equals(opinionDTO.getOnlySave())) {
			AuditProblemsDTO dto = new AuditProblemsDTO();
			dto.setProblemCode(opinionDTO.getProblemNo());
			dto.setProblemStatus(opinionDTO.getStatusAfter());
			commonDAO.update("auditProblemsDao.updateAuditProblems", dto);
			// 保存时,跳转流程
			this.auditWorkFlowService.processWorkFlow(
					opinionDTO.getProblemNo(), user, null,
					AuditConstants.TASK_TYPE_PROBLEM);
		}

		//
		// 扣分
		if ("0".equals(opinionDTO.getOnlySave())) {
			integrityMedicalService.addIntegrityPointTemps(transferJson,
					user.getUsername());// 临时扣分
			commonService.deleteAuditCasePoint(opinionDTO.getProblemNo(),
					user.getUsername());
		} else {
			commonService.saveAuditCasePoint(opinionDTO.getProblemNo(),
					transferJson, user.getUsername());
		}
		//
		return map;
	}

	public void getAuditCasePoint(AuditCaseDTO caseDTO,
			JSONObject transferJson, String userName) throws Exception {

	}

	@Override
	public List<AuditOpinionDTO> queryAllOpinion(AuditOpinionDTO opinionDTO,
			UserDetails user, String caseCode, String problemNo) {

		List<AuditOpinionDTO> list = this.commonDAO.query(
				"opinoin.queryAuditOpinoinByParameter", opinionDTO);

		HcpmsStorageDTO tempObject = new HcpmsStorageDTO();
		tempObject.setBusinessId(caseCode);
		tempObject.setModuleType(AuditConstants.jhModule);
		tempObject.setChildModuleType(AuditConstants.approvement);
		tempObject.setBusinessType(AuditConstants.opinionType_15);
		tempObject.setUserId(user.getUsername());

		Object obj = this.hcpmsStorageService.getStorageDataMap(tempObject);
		if (obj != null) {
			AuditOpinionDTO currentObj = JSONObject.toJavaObject(
					((JSONObject) obj), AuditOpinionDTO.class);

			AuditOpinionDTO currentOpinion = new AuditOpinionDTO();
			currentOpinion.setOpinion(currentObj.getOpinion());
			currentOpinion.setOpinionDesc(currentObj.getOpinionDesc());
			currentOpinion.setOpinionType(AuditConstants.opinionType_15);
			list.add(currentOpinion);
		}

		return list;

	}

	public AuditOpinionDTO getOpinionByType(AuditOpinionDTO opinionDTO) {
		AuditOpinionDTO opinion = auditOpinionDao.getOpinionByType(opinionDTO);
		return opinion;
	}

	// 暂存
	public Map<String, Object> saveStorageData(String businessId,
			String moduleType, String childModuleType, String businessType,
			String userName, Object opinionDTO, Map<String, Object> map) {
		HcpmsStorageDTO vo = new HcpmsStorageDTO();
		vo.setBusinessId(businessId);
		vo.setModuleType(moduleType);
		vo.setChildModuleType(childModuleType);
		vo.setBusinessType(businessType);
		vo.setUserId(userName);
		vo.setDataMap(opinionDTO);
		hcpmsStorageService.saveStorageData(vo, false);
		map.put("state", AuditConstants.FLAG_TURE);
		map.put("msg", AuditConstants.ZCMSG);
		return map;
	}

	// 暂存删除
	public void deleteStorageData(String businessId, String moduleType,
			String childModuleType, String businessType, String userName) {
		HcpmsStorageDTO vo = new HcpmsStorageDTO();
		vo.setBusinessId(businessId);
		vo.setModuleType(moduleType);
		vo.setChildModuleType(childModuleType);
		vo.setBusinessType(businessType);
		vo.setUserId(userName);
		hcpmsStorageService.deleteStorageData(vo);
	}

	// 扣分
	public void getAuditCasePoint(AuditOpinionDTO opinionDTO,
			JSONObject transferJson, String userName) throws Exception {
		if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "0".equals(opinionDTO.getOnlySave())) {
			integrityMedicalService.addIntegrityPointTemps(transferJson,
					userName);// 临时扣分
			this.commonService.deleteAuditCasePoint(opinionDTO.getProblemNo(),
					userName);

		} else if (StringUtils.hasText(opinionDTO.getOnlySave())
				&& "1".equals(opinionDTO.getOnlySave())) {
			this.commonService.saveAuditCasePoint(opinionDTO.getProblemNo(),
					transferJson, userName);
		}
	}

	/**
	 * 批量操作（调查）
	 * 
	 * @param vo
	 * @param request
	 * @return
	 * @throws IOException
	 * @throws Exception
	 */
	public Map<String, Object> onBatchOperateDcServ(String[] problemNos,
			UserDetails user, Map<String, Object> result) throws Exception {
		int count = 0;
		// 在查暂存表中“调查详情”和“处理意见”
		HcpmsStorageDTO vo = new HcpmsStorageDTO();
		vo.setUserId(user.getUsername());
		vo.setBusinessType(AuditConstants.opinionType_3);
		vo.setModuleType(AuditConstants.sjModule);
		vo.setChildModuleType(AuditConstants.audit);

		for (String problemNo : problemNos) {
			// 在查暂存表中“调查详情”和“处理意见”
			vo.setBusinessId(problemNo);
			JSONObject obj = (JSONObject) hcpmsStorageService
					.getStorageDataMap(vo);

			AuditOpinionDTO opinionDTO = JSONObject.toJavaObject(obj,
					AuditOpinionDTO.class);
			if (null != opinionDTO) {
				opinionDTO.setOnlySave(AuditConstants.ONLY_SAVE_0);
				opinionDTO.setDealUserName(user.getUsername());
				opinionDTO.setOpinionType(AuditConstants.opinionType_3);
				opinionDTO.setTaskType(AuditConstants.taskType_01);

				// 扣分
				JSONObject transferJson = caseCommonServiceImpl
						.queryBatchOpertionAuditCasePoint(problemNo,
								user.getUsername());
				this.processProblemAudit(opinionDTO, transferJson,
						user.getUsername(), user, result);
				//
				caseCommonServiceImpl.batchOpertionMedicalCommon(problemNo,
						AuditConstants.taskType_01, user.getUsername());
				count++;
			}
		}
		result.put("state", AuditConstants.FLAG_TURE);
		result.put("msg", "操作成功" + count + "条，失败" + (problemNos.length - count)
				+ "条");
		return result;
	}

	/**
	 * 事件调查复核
	 * **/
	public Map<String, Object> onBatchOperateReviewServ(String[] problemNos,
			AuditOpinionDTO opinionDTO, UserDetails user,
			Map<String, Object> result) throws Exception {
		int count = 0;
		AuditProblemsDTO dto = new AuditProblemsDTO();
		for (String problemNo : problemNos) {
			dto.setProblemCode(problemNo);
			// 获取当前事件状态：==‘5’才可以复核
			AuditProblemsDTO rsl = (AuditProblemsDTO) commonDAO.get(
					"auditProblemsDao.queryAuditProblemsDetail", dto);
			if (rsl.getProblemStatus().equals(AuditConstants.problemStatus_5)) {
				if (null != opinionDTO.getOpinionDesc()
						&& !"".equals(opinionDTO.getOpinionDesc())
						&& null != opinionDTO.getOpinionState()
						&& !"".equals(opinionDTO.getOpinionState())) {
					opinionDTO.setProblemNo(problemNo);
					// 扣分
					JSONObject transferJson = (JSONObject) JSONObject
							.toJSON(opinionDTO);
					// 事件调查复核
					result = this.processBackProblemReview(opinionDTO,
							transferJson, user.getUsername(), user, result);
					count++;
				}
			}
		}
		result.put("state", AuditConstants.FLAG_TURE);
		result.put("msg", "操作成功" + count + "条，失败" + (problemNos.length - count)
				+ "条");
		return result;
	}
	/**
	 *  流程流转情况详情展示
	 * **/
	public Map<String, Object> showWFHistory(String problemNo,
			Map<String, Object> result) throws Exception {
		Map<String, Object> map=new HashMap<String, Object>();
		AuditProblemsDTO dto = new AuditProblemsDTO();
		dto.setProblemCode(problemNo);
		AuditProblemsDTO rsl = (AuditProblemsDTO) commonDAO.get(
				"auditProblemsDao.queryAuditProblemsDetail", dto);
		
       map.put("codeType", AuditConstants.AUDIT_PROBLEM_STATUS);
       map.put("codeNo", "'1','2'");//手工录入
       boolean flag=true;
     //流程分两种情况：1、手工录入；2、预警转稽核
		if(null!=rsl.getOtherCaseCode() && !"".equals(rsl.getOtherCaseCode())){
			map.put("codeNo", "0");//预警转稽核
			flag=false;
		}
		List<AuditCommDTO> wfActinstList =new ArrayList<AuditCommDTO>();
		if(flag){
			 wfActinstList = commonDAO.query("workflow.queryWFActinsts", AuditConstants.PROBLE_WF_NO);
			 wfActinstList.get(0).setName("录入");
		}else{
			AuditCommDTO afCommDTO=new AuditCommDTO();
			afCommDTO.setName("问题分配");
			wfActinstList.add(afCommDTO);
			wfActinstList.addAll( commonDAO.query("workflow.queryWFActinsts", AuditConstants.PROBLE_WF_NO));
		}
		List<AuditCommDTO> wfList = commonDAO.query(
				"auditProblemsDao.queryStatus", map);
	
	   getWFHistory(wfList, wfActinstList, rsl.getProblemStatus(), problemNo,flag,result);
		return result;
	}
	public Map<String, Object> getWFHistory(List<AuditCommDTO> wfList,
			List<AuditCommDTO> wfActinstList,String problemStatus,String taskNo,boolean flag,Map<String, Object> map)
	{
		List<HcpmsWFHistoryDTO> wfHistoryList=new ArrayList<HcpmsWFHistoryDTO>();
		//获取问题当前状态
		int status=Integer.valueOf(problemStatus);
		String problemState="";
		AuditOpinionDTO opinionDTO=new AuditOpinionDTO();
		opinionDTO.setTaskType(AuditConstants.taskType_01);
		opinionDTO.setProblemNo(taskNo);
		
		for (int i = 0; i < wfList.size(); i++) {
			HcpmsWFHistoryDTO wfHistoryDTO=new HcpmsWFHistoryDTO();
			wfHistoryDTO.setDealStatus(wfList.get(i).getName());
			wfHistoryDTO.setTaskRemark(wfActinstList.get(i).getName());
			
			this.getWFhistoryDTO(wfList, wfActinstList, opinionDTO, status, wfHistoryDTO, flag, i);
			
			if(Integer.valueOf(wfList.get(i).getCode())==status){
				problemState=wfList.get(i).getName();
			}
			wfHistoryList.add(wfHistoryDTO);
		}
		map.put("problemState", problemState);
		map.put("wfHistoryList", wfHistoryList);

		return map;
	}
	
	public void getWFhistoryDTO(List<AuditCommDTO> wfList,
			List<AuditCommDTO> wfActinstList,AuditOpinionDTO opinionDTO,
			int status,HcpmsWFHistoryDTO wfHistoryDTO,boolean flag,int i)
	{
		if(Integer.valueOf(wfList.get(i).getCode())<=status){ //当流程中的状态小于等于当前状态时，则获取对应的处理人和处理日期
			opinionDTO.setOpinionType(i+1+"");
			if(!flag){
				opinionDTO.setOpinionType(i+"");
			}
			AuditOpinionDTO auditOpinionDTO = (AuditOpinionDTO) commonDAO.get("opinoin.queryOpinion", opinionDTO);
			if(null!=auditOpinionDTO){
				wfHistoryDTO.setDealName(auditOpinionDTO.getDealUserName());
				wfHistoryDTO.setDealDate(auditOpinionDTO.getDealDateStr());
			}else{
				wfHistoryDTO.setDealName(wfActinstList.get(i).getDealName());
				wfHistoryDTO.setDealDate("");
			}
		}
		else {
			wfHistoryDTO.setDealName(wfActinstList.get(i).getDealName());
			wfHistoryDTO.setDealDate("");
		}
	}
	/**
	 *  流程流转情况详情展示
	 * **/
	public Map<String, Object> showWFCaseHistory(String caseCode,String problemNo,
			Map<String, Object> result) throws Exception {
		Map<String, Object> map=new HashMap<String, Object>();
		
		AuditCaseDTO auditCaseDTO=new AuditCaseDTO();
		auditCaseDTO.setCaseCode(caseCode);
       map.put("codeType", AuditConstants.AUDIT_CASE_STATUS);
       map.put("codeNo", "'1'");//手工录入
      
		List<AuditCommDTO> wfActinstList=commonDAO.query("workflow.queryWFActinsts", AuditConstants.CASE_WF_NO);
		wfActinstList.remove(5);
		List<AuditCommDTO> wfList = commonDAO.query("auditProblemsDao.queryStatus", map);
	
		AuditCaseDTO caseDTO=(AuditCaseDTO) commonDAO.get("audticase.getCaseState", auditCaseDTO);
		 getWFCaseHistory(wfList, wfActinstList, caseDTO.getCaseState(),caseCode,problemNo,result);
		return result;
	}
	public Map<String, Object> getWFCaseHistory(List<AuditCommDTO> wfList,
			List<AuditCommDTO> wfActinstList,String getCaseState,
			String caseCode,String problemNo,Map<String, Object> map)throws Exception
	{
		List<HcpmsWFHistoryDTO> wfHistoryList=new ArrayList<HcpmsWFHistoryDTO>();
		
		this.showWFHistory(problemNo, map);
		wfHistoryList.addAll((List<HcpmsWFHistoryDTO> )map.get("wfHistoryList"));
	
		//获取问题当前状态
		int status=Integer.valueOf(getCaseState);
		String problemState="";
		AuditOpinionDTO opinionDTO=new AuditOpinionDTO();
		opinionDTO.setTaskType(AuditConstants.taskType_02);
		opinionDTO.setProblemNo(caseCode);
		
		for (int i = 0; i < wfList.size(); i++) {
			HcpmsWFHistoryDTO wfHistoryDTO=new HcpmsWFHistoryDTO();
			wfHistoryDTO.setDealStatus(wfList.get(i).getName());
			if(i<wfActinstList.size())
			{
				wfHistoryDTO.setTaskRemark(wfActinstList.get(i).getName());
			}
			this.getWFCaseHistoryDTO(wfList, wfActinstList, opinionDTO, wfHistoryDTO, status, i);
			
			if(Integer.valueOf(wfList.get(i).getCode())==status){
				problemState=wfList.get(i).getName();
			}
			wfHistoryList.add(wfHistoryDTO);
		}
		map.put("caseState", problemState);
		map.put("wfHistoryList", wfHistoryList);

		return map;
	}
	public void getWFCaseHistoryDTO(List<AuditCommDTO> wfList,
			List<AuditCommDTO> wfActinstList,AuditOpinionDTO opinionDTO,
			HcpmsWFHistoryDTO wfHistoryDTO,int status,int i)
	{
		if(Integer.valueOf(wfList.get(i).getCode())<=status){ //当流程中的状态小于等于当前状态时，则获取对应的处理人和处理日期
			if(i==2){
				opinionDTO.setOpinionType(i+12+"");
			}else if(i==3){
				opinionDTO.setOpinionType(i+10+"");
			}else{
				opinionDTO.setOpinionType(i+11+"");
			}
			if(i<wfActinstList.size()){
				AuditOpinionDTO auditOpinionDTO = (AuditOpinionDTO) commonDAO.get("opinoin.queryOpinion", opinionDTO);
				if(null!=auditOpinionDTO){
					wfHistoryDTO.setDealName(auditOpinionDTO.getDealUserName());
					wfHistoryDTO.setDealDate(auditOpinionDTO.getDealDateStr());
				}else{
					wfHistoryDTO.setDealName(wfActinstList.get(i).getDealName());
					wfHistoryDTO.setDealDate("");
				}
			}
		}
		else if(i<wfActinstList.size() ){
			wfHistoryDTO.setDealName(wfActinstList.get(i).getDealName());
			wfHistoryDTO.setDealDate("");
		}
	}
}
